(function () {
    var pos = 1
    var paId
    var count

    $('#query').on('click', function () {
        paId = $('#paId').val();
        var paPassword = $('#paPassword').val();
        if (!/^\d{1,16}$/.test(paId)){
            layer.tips('ID不正确！', '#paId', {tips:[1,'rgba(0,0,0,0.5)'], time:3000})
            return;
        }
        if (!/^\w{2,16}$/.test(paPassword)){
            layer.tips('密码不正确！', '#paPassword', {tips:[1,'rgba(0,0,0,0.5)'], time:3000})
            return;
        }
        $.ajax({
            url: '/query',
            method: 'post',
            data: {paId: paId, paPassword: paPassword},
            success: function (jsonResult) {
                if (jsonResult.status == 1) {
                    // layer.msg(jsonResult.message, {icon: 6})
                    $(".form-inline").hide()
                    $('article>hr').removeClass('hide')
                    var paName = jsonResult.data.paName;
                    var singleCount = jsonResult.data.singleCount;
                    var yesnoCount = jsonResult.data.yesnoCount;
                    count = singleCount + yesnoCount
                    $('article>h5').removeClass('hide').text('试卷名：'+paName+' 单选题：'+singleCount+' 判断题：'+yesnoCount)
                    cur()
                }
                else layer.msg(jsonResult.message, {icon: 5})
            },
            error: function () {
                layer.msg('服务器异常！')
            }
        })
    })
    function cur() {
        $.ajax({
            url: '/question',
            method: 'get',
            data: {paId: paId, pos: pos},
            success: function (jsonResult) {
                if (jsonResult.status == 1) {
                    // layer.msg(jsonResult.message, {icon: 6})
                    $('#single').addClass('hide')
                    $('#yesno').addClass('hide')
                    $('article>div').removeClass('hide')
                    $('dl dd input').prop('checked', false)
                    var ques = jsonResult.data.ques;
                    if(jsonResult.data.type == 'single'){
                        $('#single').removeClass('hide')
                        $('#single>dt').text(ques.sinPos+'.'+ques.sinCaption+'<'+ques.sinPower+'分>')
                        $('#single>dd>label:eq(0)').text('a.'+ques.sinA)
                        $('#single>dd>label:eq(1)').text('b.'+ques.sinB)
                        $('#single>dd>label:eq(2)').text('c.'+ques.sinC)
                        $('#single>dd>label:eq(3)').text('d.'+ques.sinD)
                        $('dl dd input[value=' + jsonResult.data.sel + ']').prop('checked', jsonResult.data.sel != null)
                    }
                    if(jsonResult.data.type == 'yesno'){
                        $('#yesno').removeClass('hide')
                        $('#yesno>dt').text(ques.ynPos+'.'+ques.ynCaption+'<'+ques.ynPower+'>')
                        $('dl dd input[value=' + jsonResult.data.sel + ']').prop('checked', jsonResult.data.sel != null)
                    }
                }
                else layer.msg(jsonResult.message, {icon: 5})
            },
            error: function () {
                layer.msg('服务器异常！')
            }
        })
    }
    $('dl dd input').on('click', function () {
        $.ajax({
            url: '/answer',
            method: 'post',
            data: {paId: paId, pos: pos, ansSelect: this.value},
            success: function () {
            },
            error: function () {
                layer.msg('服务器异常！')
            }
        })
    })
    $('article>div>button').on('click', function () {
        if (this.id == 'prev'){
            if (pos <= 1) return layer.msg('已经是第一题！')
            pos--
            cur()
        } else if (this.id == 'next'){
            if (pos >= count) return layer.msg('已经是最后一题！')
            pos++
            cur()
        } else if (this.id == 'handPaper'){
            layer.confirm('您确定要交卷吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    url: '/hand',
                    method: 'post',
                    data: {paId: paId},
                    success: function () {
                        layer.msg('交卷成功', {icon: 1});
                    },
                    error: function () {
                        layer.msg('服务器异常！')
                    }
                })
            });
        }
    })
})();